﻿using System;
using System.Collections.Generic;
using System.IO;

namespace GnuPGNotepad
{
    public class Settings : IDisposable
    {
        #region Constructor
        public Settings() { loadSettings(); }
        #endregion // Constructor

        #region Public Declarations
        public bool IsDisposed { get; private set; }

        public string Recipient { get; set; }
        public List<string> Recipients { get; set; }
        public string Passphrase { get; set; }
        public string ActiveFile { get; set; }
        public string ActiveFileName
        {
            get
            {
                if (null == this.ActiveFile)
                    return null;

                FileInfo file = new FileInfo(this.ActiveFile);
                return file.Name;
            }
        }
        public string ActiveFilePath
        {
            get
            {
                if (null == this.ActiveFile)
                    return null;

                FileInfo file = new FileInfo(this.ActiveFile);
                return file.DirectoryName;
            }
        }
        public int ApplicationTimeout
        {
            get { return applicationTimeout; }
            set { applicationTimeout = value; }
        }

        // app.config settings variables
        public string GPGBinaryPath { get; set; }
        public string HomeDirectory { get; set; }
        public bool SaveBinaryFormat { get; set; }

        #endregion // Public Declarations

        #region Private Declatations
        /// <summary>
        /// Set the default application timeout to 15 seconds
        /// </summary>
        private int applicationTimeout = 15 * 1000; // default thread timeout (ms) value

        /// <summary>
        /// set default paths for GnuPG binaries
        /// </summary>
        private static string[] defaultAppPaths = { 
                "C:\\Program Files\\GNU\\GnuPG\\gpg2.exe", 
                    "C:\\Program Files\\GNU\\GnuPG\\gpg.exe",
                        "C:\\Program Files (x86)\\GNU\\GnuPG\\gpg2.exe", 
                            "C:\\Program Files (x86)\\GNU\\GnuPG\\gpg.exe" };

        #endregion // Private Declatations

        #region Private Methods
        /// <summary>
        /// load saved application settings 
        /// </summary>
        private void loadSettings()
        {
            // load settings from user config if available
            getBinaryPath();
            
            // load recipients from user config if available
            getRecipients();

            // load application timeout value from user config if available
            getApplicationTimeout();

            SaveBinaryFormat = Properties.Settings.Default.SaveBinaryFormat || false;
        }

        private void getHomeDirectory()
        {
            string homedirectory = Properties.Settings.Default.HomeDirectory;

            if (!string.IsNullOrEmpty(homedirectory))
                HomeDirectory = homedirectory;
        }

        /// <summary>
        /// load the application timeout value
        /// </summary>
        private void getApplicationTimeout()
        {
            if (Properties.Settings.Default.ApplicationTimeout != 0)
                ApplicationTimeout = Properties.Settings.Default.ApplicationTimeout;
        }

        /// <summary>
        /// looks for a valid binary path and loads the first successful path
        /// </summary>
        private void getBinaryPath()
        {
            string binaryPath = Properties.Settings.Default.GPGBinaryPath;

            if (string.IsNullOrEmpty(binaryPath))
                foreach (string path in defaultAppPaths)
                    if (File.Exists(path))
                    {
                        binaryPath = path;
                        break;
                    }

            GPGBinaryPath = binaryPath;
        }

        /// <summary>
        /// load any saved recipients into config
        /// </summary>
        private void getRecipients()
        {
            // initialize Recipients array
            Recipients = new List<string>();
            
            // if nothing loaded then exit
            if (string.IsNullOrEmpty(Properties.Settings.Default.Recipients))
                return;
            
            // recpients found in config - trim and load each into array
            foreach (string recipient in Properties.Settings.Default.Recipients.Split(','))
                Recipients.Add(recipient.Trim());
        }

        #endregion // Private Methods

        #region IDisposable Members
        /// <summary>
        /// Dispose of objects
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Dispose of objects in class
        /// </summary>       
        protected virtual void Dispose(bool disposing)
        {
            if (!IsDisposed)
                if (disposing)
                {
                    // dispose objects
                    Recipient  = null;
                    Recipients = null;
                    Passphrase = null;
                    ActiveFile = null;

                    // mark disposed
                    IsDisposed = true;
                }
        }

        /// <summary>
        /// Destructor method class
        /// </summary>
        ~Settings()
        {
            Dispose(false);
        }
        #endregion // IDisposable Members
    }

    public enum PGPAction
    { Encrypt, SignAndEncrypt, Decrypt, Sign, Verify }

}
